﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEM_DAL
{
    public class CargoDAL
    {
        SqlConnection cnn;
        public Boolean GuardarCargo(Cargo elCargo) {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();

                SqlCommand cmd = new SqlCommand("RegistrarCargo",cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                
                cmd.Parameters.Add("@CargoID", SqlDbType.Int);
                cmd.Parameters["@CargoID"].Direction = ParameterDirection.InputOutput;
                cmd.Parameters.Add("@NomCargo", SqlDbType.NVarChar,50);
                cmd.Parameters.Add("@DescCargo", SqlDbType.Text);
                cmd.Parameters.Add("@FecReg", SqlDbType.NVarChar,12);
                cmd.Parameters["@CargoID"].Value = elCargo.CargoID;
                cmd.Parameters["@NomCargo"].Value = elCargo.NomCar;
                cmd.Parameters["@DescCargo"].Value = elCargo.DescripCar;

                cmd.ExecuteNonQuery();
                elCargo.CargoID = (Int32)(cmd.Parameters["@CargoID"].Value);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }
        public DataTable ObtenerCargo() {
            DataTable lista = null;
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();

                SqlDataAdapter da = new SqlDataAdapter("ObetnerCargo",cnn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                lista = new DataTable();
                da.Fill(lista);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return lista;
        }
        public Boolean ActualizarCargo(Cargo elCargo)
        {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ActulizarCargo", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@CargoID", SqlDbType.Int);
                cmd.Parameters.Add("@NomCargo", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@DescCargo", SqlDbType.Text);
                cmd.Parameters.Add("@FecReg", SqlDbType.NVarChar,12);
                cmd.Parameters["@CargoID"].Value = elCargo.CargoID;
                cmd.Parameters["@NomCargo"].Value = elCargo.NomCar;
                cmd.Parameters["@DescCargo"].Value = elCargo.DescripCar;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e) { throw new Exception(e.Message); }
            catch (Exception ex) { throw new Exception(ex.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }
    }
}
